layui.config({
    base: '../../layui/' //此处路径请自行处理, 可以使用绝对路径
}).extend({
    formSelects: 'formSelects-v4'
}).use(['jquery', 'formSelects', 'form','layer'], function () {
    const form = layui.form
        , layer = layui.layer
        , $ = layui.jquery;
    $("#close").on('click', function () {
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    form.on('submit(save)', function (data) {
        const params = data.field;
        submit($, params);
        return false;
    });

    function submit($, params) {
        $.ajax({
            url: '/admNamespace',
            data: JSON.stringify(params),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            type: "POST",
            success: function (result) {
                layer.msg(result.data, {icon: 1, time: 1000}, function () {
                    CloseWin();
                })
            }
        })
    }
    //关闭页面
    function CloseWin() {
        parent.location.reload(); // 父页面刷新
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }

    $(function () {
        getDockerRegistryData("")
    })

    registryNameInput = function(e){
        const val = $("#registryName").val();
        getDockerRegistryData(val)
    }
    let dockerRegistryData
    function getDockerRegistryData(registryName) {
        $.ajax({
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/admDockerRegistry/getAll?name="+registryName,//url
            success: function (result) {
                dockerRegistryData = result
                $("#registryId").html("");
                var html = "";
                $("#registryId").html("<option value=\"\">请选择仓库</option>");
                if (result != null) {
                    $.each(result, function (index, item) {
                        html += "<option  value='" + item.id + "'>" + item.registryName + "</option>";
                    })
                }
                $("#registryId").append(html);
                form.render('select');
                if (registryName != null && registryName !=''){
                    $(".registryId").find(".layui-form-select").addClass("layui-form-selected")
                }
            },
            error: function () {
                alert("异常！");
            }
        });
    }
    // 监听下拉框改变事件,修改输入框中的内容
    form.on("select(registryId)",function(data){
        let value=data.value
        let selectObj=dockerRegistryData.filter(item=>{
            return item.id==value
        })
        $("#registryName").val(selectObj[0].registryName);
    })
});

